package com.ihr360.payroll.model.dto.salary.report;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Lists;
import com.ihr360.authority.sharing.entityhandler.UserResourceHandler;
import com.ihr360.commons.lang.CollectionUtils;
import com.ihr360.payroll.model.dto.salary.report.embedded.MergeSalaryField;
import com.ihr360.shared.entitywithlistener.NumberIdWithTimeAndCompanyId;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.Type;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.OneToMany;
import java.util.List;

/**
 * Created by Eric on 2018/4/12/012.
 */

/**
 * 合并报表
 */
@Entity
@Getter
@Setter
@EntityListeners(value = {UserResourceHandler.class})
public class MergeSalaryReport extends NumberIdWithTimeAndCompanyId<Long> {
    public static class JoinType {
        /**
         * 以表为基准
         */
        public final static String LEFT_JOIN = "LEFT_JOIN";
        /**
         * 取各张表都有的员工
         */
        public final static String INNER_JOIN = "INNER_JOIN";
        /**
         * 取各张表中所有的员工
         */
        public final static String CROSS_JOIN = "CROSS_JOIN";
    }

    /**
     * 合并报表名称
     */
    @Column(length = 50, nullable = false)
    private String name;

    /**
     * 是否每月发放一次
     */
    private boolean monthCycle = true;

    /**
     * 合并的薪资方案
     */
    @Column(columnDefinition = "text", name = "salaryPlanList")
    @Type(type = "json")
    private List<String> salaryPlanList;

    /**
     * 合并的薪资方案id关系表
     */
    @OneToMany(mappedBy = "mergeReport", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<MergeReportWithSalaryPlan> planIdList;

    /**
     * 备注
     */
    @Column(length = 100)
    private String remark;

    /**
     * 链接方式
     */
    @Column(length = 20, nullable = false)
    private String joinType;

    /**
     * 基准薪资报表id（joinType 以表为基准方式 才存在）
     */
    @Column
    private Long joinSalaryPlanId;

    /**
     * 选择字段
     */
    @Column(columnDefinition = "text", name = "mergeSalaryField")
    @Type(type = "json")
    private MergeSalaryField mergeSalaryField;

    /**
     * 是否是合并计税
     */
    @Column
    private Boolean isMergeTax = false;

    @JsonIgnore
    public List<Long> getSalaryPlanIdList() {
        List<Long> salaryPlanIdList = Lists.newArrayList();
        if (CollectionUtils.isEmpty(salaryPlanList)) {
            return salaryPlanIdList;
        }
        for (String salaryPlanId : salaryPlanList) {
            salaryPlanIdList.add(Long.parseLong(salaryPlanId));
        }
        return salaryPlanIdList;
    }

    public List<MergeReportWithSalaryPlan> getPlanIdList() {
        return planIdList;
    }

    public void setPlanIdList(List<MergeReportWithSalaryPlan> planIdList) {
        this.planIdList = planIdList;
    }
}
